package all_in_one;

import java.util.concurrent.Semaphore;

public class GrabM extends Thread
{

	private CigarettePlace m_place;
	private Semaphore m_mutex;
	
	public GrabM(CigarettePlace cigarettePlace, Semaphore mutex)
	{
		m_place = cigarettePlace;
		m_mutex = mutex;
	}
	
	@Override
	public void run()
	{
		while(true)
		{
			Grab();
		}
	}
	public synchronized void Grab() {
		m_place.GetMatchSemaphore().acquireUninterruptibly();
		System.out.println("A match was taken");
		m_mutex.acquireUninterruptibly();
	
		m_place.GetCounter().Add(4);
	
		m_place.GetSemaphoreArray().get(m_place.GetCounter().GetValue()-1).release();
		m_mutex.release();
	}
	
}
